/**
 * 
 * 功能：检查各种输入内容是否符合要求，如用户名以英文字母开头的4-20个字母、数字或中文，密码6-20个英文字母或者数字等
 * 
 * @author 王晓霞
 * 
 * 关键字：CNIIA: WangXiaoxia add login & register@2013/7/17
 * 
 */
package com.v2future.sjt.collegecheckingattendance.utils;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.Toast;

import java.io.UnsupportedEncodingException;

public class CheckInput {

	private Context mContext = null;

	public CheckInput(Context context) {
		this.mContext = context;
	}

	/**
	 * 用正则表达式检查身份证号输入是否符合要求
	 * 
	 * @param idnumber
	 */
	public static boolean isIdnumberOK(String id_number) {
		return id_number.matches("^\\d{15}(\\d{2}[0-9xX])?$");
	}

	/**
	 * 用正则表达式检查email输入是否符合要求
	 * 
	 * @param email
	 */
	public static boolean isEmailOK(String email) {
		return email
				.matches("^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$");
	}

	/**
	 * 用正则表达式检查手机号输入是否符合要求:以1开头的11位数字
	 * 
	 * @param phone_number
	 */
	public static boolean isPhoneNumberOK(String phone_number) {
		if (!TextUtils.isEmpty(phone_number)) {
			return phone_number.matches("^1\\d{10}$");
		}
		return false;
		// return phone_number.matches("0?1[3,4,5,8][0-9]{9}");
		// return phone_number.matches("1{11}");
	}

	/**
	 * 用正则表达式检查电话号码输入是否符合要求:只能数字
	 * 
	 * @param phone_number
	 */
	public static boolean isTelNumberOK(String tel_number) {
		return tel_number.matches("^\\d+$");
	}

	/**
	 * 检查用户名输入是否符合要求 应为：以英文字母开头的4-20个字母、数字或中文
	 * 
	 * @param user_name
	 */
	public boolean isUsernameOK(String user_name) {
		if (user_name.length() < 4) {
			Toast.makeText(mContext, "用户名不能小于4位！", Toast.LENGTH_LONG).show();
			return false;
		}

		if (user_name.length() > 20) {
			Toast.makeText(mContext, "用户名不能大于20位！", Toast.LENGTH_LONG).show();
			return false;
		}

		/* 以英文字母开头的4-20个字母、数字或中文 */
		if (user_name.matches("^[a-zA-Z]{1}[\u4e00-\u9fa5_a-zA-Z0-9_]{3,19}$")) {
			return true;
		} else {
			Toast.makeText(mContext, "用户名输入有误，请检查！", Toast.LENGTH_LONG).show();
			return false;
		}

	}

	/**
	 * 检查密码输入是否符合要求 应为：6~20个英文字母或数字
	 * 
	 * @param pw
	 */
	public boolean isPwOK(String pw) {

		if (pw.length() < 6) {
			Toast.makeText(mContext, "密码不能小于6位！", Toast.LENGTH_LONG).show();
			return false;
		}

		if (pw.length() > 20) {
			Toast.makeText(mContext, "密码不能大于20位！", Toast.LENGTH_LONG).show();
			return false;
		}

		/* 判断是否只包含数字、字母 */
		for (int i = 0; i < pw.length(); i++) {
			char c = pw.charAt(i);
			if (!(Character.isDigit(c) || ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))) {
				Toast.makeText(mContext, "密码只能为英文字母或数字！", Toast.LENGTH_LONG)
						.show();
				return false;
			}
		}

		return true;
	}

	/**
	 * 检查确认密码是否和密码一致
	 * 
	 * @param pw_confirm
	 *            pw
	 */
	public boolean isPwConfirmOK(String pw_confirm, String pw) {

		if (!(pw_confirm.compareTo(pw) == 0)) {
			Toast.makeText(mContext, "两次密码输入不一致！请重新输入。", Toast.LENGTH_LONG)
					.show();
			return false;
		}

		return true;
	}

	/**
	 * 限定字符串长度，超出部分替换成制定字符串
	 * 
	 * @param originalString
	 *            原始串
	 * @param len
	 *            限定长度
	 * @param replaceString
	 *            替换的串
	 * @return
	 */
	public static String changStrLenReplaceStr(String originalString, int len,
											   String replaceString) {
		if (null == originalString || "".equals(originalString)) {
			return originalString;
		}

		if (originalString.length() <= len) {
			return originalString;
		} else {
			String result = originalString.substring(0, len);
			result = result + replaceString;
			return result;
		}
	}

	/**
	 * 判断身份证姓名
	 * 
	 * @return
	 */
	public static boolean isCardName(String originalName) {
		boolean flag = false;
		if (!TextUtils.isEmpty(originalName)) {
			try {
				if (originalName.getBytes("gbk").length >= 1
						&& originalName.getBytes("gbk").length <= 26) {

					flag = isChineseOnly(originalName)
							|| isEnglishOnly(originalName)
							|| isEnglishChinese(originalName);
				} else {
					flag = false;
				}
			} catch (Exception e) {

			}
		}

		return flag;
	}

	/**
	 * 判断昵称是否符合要求
	 * 
	 * @param nickname
	 * @return
	 */
	public static boolean isNickname(String nickname) {

		boolean flag = false;

		if (!TextUtils.isEmpty(nickname)) {
			try {
				if (nickname.getBytes("gbk").length >= 4
						&& nickname.getBytes("gbk").length <= 30) {

					flag = nickname.matches("[\u4E00-\u9FA5a-zA-Z-_0-9]{2,30}");
				} else {
					flag = false;
				}
			} catch (UnsupportedEncodingException e) {

			}
		}

		return flag;
	}

	// 是否全部为英文
	public static boolean isEnglishOnly(String originalString) {
		String patrn = "^[a-zA-Z]+$";
		return originalString.matches(patrn);
	}

	// 包含中英文
	public static boolean isEnglishChinese(String originalString) {
		String patrn = "^[\u4E00-\u9FA5a-zA-Z]+$";
		return originalString.matches(patrn);
	}

	// 校验是否为全部中文
	public static boolean isChineseOnly(String originalString) {
		String patrn = "^[\u4E00-\u9FA5]+$";
		return originalString.matches(patrn);
	}

	// 校验是否全是小写字母
	public static boolean isa_z(String originalString) {
		String patrn = "^[a-z]+$";
		return originalString.matches(patrn);
	}

	// 校验是否全是大写字母
	public static boolean isUpperA_Z(String originalString) {
		String patrn = "^[A-Z]+$";
		return originalString.matches(patrn);
	}

	// 校验是否全是数字
	public static boolean isDigit(String originalString) {
		String patrn = "^\\d+$";
		return originalString.matches(patrn);
	}

	// 校验是否全是非字母字符
	public static boolean isW(String originalString) {
		String patrn = "^[\\W_]+$";
		return originalString.matches(patrn);
	}

	// 限定小数点后是否保留两位
	public static void limitFloatPoint_2(final EditText editText) {
		editText.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
									  int count) {
				if (s.toString().contains(".")) {
					if (s.length() - 1 - s.toString().indexOf(".") > 2) {
						s = s.toString().subSequence(0,
								s.toString().indexOf(".") + 3);
						editText.setText(s);
						editText.setSelection(s.length());
					}
				}
				if (s.toString().trim().substring(0).equals(".")) {
					s = "0" + s;
					editText.setText(s);
					editText.setSelection(2);
				}

				if (s.toString().startsWith("0")
						&& s.toString().trim().length() > 1) {
					if (!s.toString().substring(1, 2).equals(".")) {
						editText.setText(s.subSequence(0, 1));
						editText.setSelection(1);
						return;
					}
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
										  int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}

		});

	}
}
